package com.bocloud.cmp.web.controller.app.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.controller.BasicController;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;
/**
 * @author Zaney
 * @data:2017年3月28日
 * @describe:openshift集群web控制层
 */
@RestController
@RequestMapping("/openshift/cluster")
public class OpenshiftClusterController {
	private final String BASE_SERVICE = "/openshift/cluster";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private BasicController basicController;
	@Autowired
	private ServiceFactory serviceFactory;
	
	/**
	 * 创建集群
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request){
		return basicController.create(params, BASE_SERVICE, request, OpenshiftClusterController.class.getSimpleName());
	}
	/**
	 * 统计
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/count", method = { RequestMethod.POST})
	public BsmResult count(HttpServletRequest request){
		String url = BASE_SERVICE + "/count";
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.BASIC, null, null, request);
		return service.invoke();
	}
	/**
	 * 更新
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request){
		return basicController.modify(params, BASE_SERVICE, request, OpenshiftClusterController.class.getSimpleName());
	}
	/**
	 * 列表展示
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param simple
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
		return basicController.list(page, rows, params, sorter, simple, BASE_SERVICE, request,
				OpenshiftClusterController.class.getSimpleName());
	}
	/**
	 * 详情
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request){
		return basicController.detail(params, BASE_SERVICE, request, OpenshiftClusterController.class.getSimpleName());
	}
	/**
	 * 删除
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/remove";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}
	@RequestMapping(value = "/listOpenshiftClusterByAppId", method = { RequestMethod.POST })
	public BsmResult listOpenshiftClusterByAppId(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) String appId,
			HttpServletRequest request) {
			String url = BASE_SERVICE + "/listOpenshiftClusterByAppId";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			paramMap.put("appId", appId);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
	}
	
	/**
	 * master namespace、username校验
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/master/check", method = {RequestMethod.POST })
	public BsmResult checkMaster(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request){
		String url = BASE_SERVICE + "/master/check";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OTHERS, null, paramMap,
				request);
		return service.invoke();
	}
	
	/**
	 * 添加节点
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/create/node", method = { RequestMethod.POST })
	public BsmResult addNode(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		String url = BASE_SERVICE + "/create/node";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
				request);
		return service.invoke();
	}
	/**
	 * 移除节点
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/node", method = { RequestMethod.DELETE })
	public BsmResult delNode(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		String url = BASE_SERVICE + "/node";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.REMOVE, null, paramMap,
				request);
		return service.invoke();
	}
}
